<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<h1 class="page-title">${listTitle}</h1>
<s:if test="#request.salaryIndividualTaxRadix!=null">
    <input type="hidden" id="isSaveCurErea_${organizationId}" value="1"/>

    <div style="padding-bottom:20px">个税扣款基数: <input type="text" name="radix" id="radix_${organizationId}"
                                                    value="${salaryIndividualTaxRadix.radix}"> <a href="javascript://"
                                                                                                  onclick="checkRadix();"
                                                                                                  class="button"><span
            class="icon" style="background:url(${ctx}/images/img/page_edit.png)">&nbsp;</span>修改</a></div>
</s:if>
<s:else>
    <div style="padding-bottom:20px">个税扣款基数: <input type="text" name="radix" id="radix_${organizationId}"> <a
            href="javascript://" onclick="checkRadix();" class="button"><span class="icon"
                                                                              style="background:url(${ctx}/images/img/page_edit.png)">&nbsp;</span>修改</a>
    </div>
    <input type="hidden" id="isSaveCurErea_${organizationId}" value="0"/>
</s:else>
<div id="demo" class="clearfix">
    <table class="display" id="example">
        <thead>
        <tr>
            <th>选择</th>
            <th><strong>薪资扣款区间</strong></th>
            <th><strong>税率</strong></th>
            <th><strong>速扣值</strong></th>
            <th><strong>备注</strong></th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <s:if test="#request.salaryIndividualTaxRules==null || #request.salaryIndividualTaxRules.size==0">
            <tr class="gradeX">
                <td colspan="6">无相关数据</td>
            </tr>
        </s:if>
        <s:else>
            <s:iterator value="#request.salaryIndividualTaxRules" status="sta" var="rule">
                <s:if test="#request.sta.even">
                    <tr class="gradeX">
                </s:if><s:else><tr class="gradeC"></s:else>
                <td><input type="checkbox" name="tax_rule_${organizationId}" value="${rule.id}"/></td>
                <td>${rule.amountStart}-${rule.amountEnd}</td>
                <td><fmt:formatNumber type="percent" value="${rule.taxRate/100}"></fmt:formatNumber></td>
                <td>${rule.fastCutValue}</td>
                <td>${rule.remark}</td>
                <td style="width:25%"><a class="button"
                                         href="#salary/individual_tax_rule_edit.xhtml?id=${rule.id}"><span
                        style="background:url(${ctx}/images/img/page_edit.png)" class="icon">&nbsp;</span>修改</a></td>
                </tr>
            </s:iterator>
        </s:else>
        </tbody>
    </table>


    <div class="tabs side grid_12 leading">
        <div class="clear"></div>
        <div class="grid_12">
            <div class="form-action clearfix">
                <p>
                    <input type="checkbox" id="form-updates2" value="1"
                           onchange="checkAll(this,'tax_rule_${organizationId}')"/>全选

                    <a class="button" href="javascript:void(0)" onclick="deleteIndividualTaxcase();"><span
                            style="background:url(${ctx}/images/img/cancel.png)"
                            class="icon">&nbsp;</span>批量删除</a>
                    <a href="javascript:void(0)" onclick="checkBeforeSave();" class="button"><span
                            class="icon"
                            style="background:url(${ctx}/images/img/add.png)">&nbsp;</span>添加</a>
                    <a class="button" href="#salary/individual_tax_main.xhtml"><span
                            style="background:url(${ctx}/images/img/arrow_undo.png)" class="icon">&nbsp;</span>返回</a>
                </p>
            </div>
        </div>
    </div>

    <script type="text/javascript">
        //删除个税方案
        function deleteIndividualTaxcase() {
            var checkIds = new Array();
            $("input[type='checkbox'][name='tax_rule_${organizationId}']").each(function() {
                if (this.checked) {
                    checkIds.push(this.value);
                }
            });
            if (checkIds.length == 0) {
                alert("请选择要删除的项");
                return;
            }
            if (confirm("确定要删除选中的数据吗？删除后将无法恢复!")) {
                doDeleteTaxRulecases(checkIds.join(","))
            }
        }

        function doDeleteTaxRulecases(ids) {
            var url = "${ctx}/salary/individual_tax_rule_remove.xhtml";
            $.ajax(url, {
                type: "POST",
                data:{"ruleIds":ids},
                dataType:"html",
                cache:false,
                success:function(a) {
                    return pageDownloaded(a, url);
                },
                complete:function() {

                }
            });

        }

        function checkRadix() {
            if ($("#radix_${organizationId}").val() == null
                    || $("#radix_${organizationId}").val().trim() == ""
                    || isNaN($("#radix_${organizationId}").val())
                    || parseFloat($("#radix_${organizationId}").val().trim()) < 0) {
                alert("请输入有效的扣款基数");
                return;
            }

            var url = "${ctx}/salary/individual_tax_radix_modify.xhtml";
            $.ajax(url, {
                type: "POST",
                data:{"orgId":${organizationId},"radix":$("#radix_${organizationId}").val()},
                dataType:"html",
                cache:false,
                success:function(a) {
                    var res = eval("(" + a + ")");
                    if (res.availability) {
                        alert('扣款基数更新成功');
                        //window.location.href.refresh();
                    } else {
                        alert('扣款基数更新失败');
                    }
                },
                error : function(XMLHttpRequest, textStatus,
                                 errorThrown) {
                    alert('更新基数发生异常');
                },
                complete:function() {

                }
            });
        }

        function checkBeforeSave() {
            var isSave = $("#isSaveCurErea_${organizationId}").val();
            if (isSave == "1") {
                window.location.href = '${ctx}/index.xhtml#salary/individual_tax_rule_edit.xhtml?t=<%=new java.util.Date().getTime()%>&organizationId=' +${organizationId};
            } else {                            
                alert('请先填写扣款基数');
            }
        }
    </script>

